package nautilus.moneykeeper.fragment;

import java.util.ArrayList;

import nautilus.moneykeeper.MainBaseSlidingActvity;
import nautilus.moneykeeper.R;
import nautilus.moneykeeper.adapter.ListTransAdapter;
import nautilus.moneykeeper.dao.AccountDAO;
import nautilus.moneykeeper.dto.Account;
import nautilus.moneykeeper.dto.Transaction;
import nautilus.moneykeeper.system.Constant;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class AccountDetailFragment extends AbstractMainContentFragment {

	private Account _thisAccount;
	private ArrayList<Transaction> trans;
	private ListView _listTrans; 
	private ListTransAdapter _tranAdapter = null;
	private TextView tvAccountName, tvAccountNumber, tvAccountType;
	private TextView tvAccountBalance, tvCurrencyUnit;
	private boolean isButtonClicking;
	
	private Handler _handler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			switch(msg.what){
				case Constant.MSG_LOAD_DATA_DONE:
					bindData();
					break;
					
				case Constant.MSG_LOAD_DATA_ERROR:
					break;
			}
			return false;
		}
	});
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedState){
		View view = inflater.inflate(R.layout.list_trans, parent, false);
		tvAccountName = (TextView)view.findViewById(R.id.account_item_name);
		tvAccountBalance = (TextView)view.findViewById(R.id.account_item_balance);
		_thisAccount = (Account)getArguments().getSerializable(Constant.EXTRA_ACCOUNT);
		
		View header = View.inflate(getActivity(), R.layout.list_trans_header, null); 
		_listTrans = (ListView)view.findViewById(R.id.listview_trans);
		_listTrans.addHeaderView(header);
		_listTrans.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
				//if the list has header, so the row at 0 is the header
				//Right now, I don't want to do anything if user click on header
				if(isButtonClicking || pos<=0)
					return;
				isButtonClicking = true;
					
				Bundle args = new Bundle();
				args.putSerializable(Constant.EXTRA_ACCOUNT, _thisAccount);
				args.putSerializable(Constant.EXTRA_TRANSACTION, _tranAdapter.getItem(pos-1));
				((MainBaseSlidingActvity)getActivity()).setMainFragment(TranFragment.class, 
						R.anim.slide_in_from_right, R.anim.slide_out_to_left, args);
				
			}
		});
		
		tvAccountName.setText(_thisAccount.getName());
		tvAccountBalance.setText(_thisAccount.getBalance()+"");
		
		Thread t = new Thread(new Runnable(){
			@Override
			public void run() {
				loadData();
				_handler.sendEmptyMessage(Constant.MSG_LOAD_DATA_DONE);
			}
		});
		t.start();
		
		return view;
	}
	
	public void onResume(){
		isButtonClicking = false;
		super.onResume();
	}
	
	private void loadData(){
		AccountDAO dao = new AccountDAO(getActivity());
		trans = dao.getAllTransactionByAccount(_thisAccount);		
		dao.close();
	}
	
	private void bindData(){		
		if(_tranAdapter == null){
			android.util.Log.i("AccountDetailFragment", "Binding data to list  " + (trans==null?0:trans.size()) );
			_tranAdapter = new ListTransAdapter(getActivity(), trans);
			_listTrans.setAdapter(_tranAdapter);
			return;
		}
		_tranAdapter.refreshData(trans);
	}
	
	@Override
	public void onClick(View v) {
		Bundle args = null;
		switch(v.getId()){
			case R.id.btn_add_transaction:
				args = new Bundle();
				args.putSerializable(Constant.EXTRA_ACCOUNT, _thisAccount);
				((MainBaseSlidingActvity)getActivity()).setMainFragment(TranFragment.class, 
						R.anim.slide_in_from_right, R.anim.slide_out_to_left, args);
				break;
			
			case R.id.btn_update_account:
				args = new Bundle();
				args.putSerializable(Constant.EXTRA_ACCOUNT, _thisAccount);
				((MainBaseSlidingActvity)getActivity()).setMainFragment(UpdateAccountFragment.class, 
						R.anim.slide_in_from_right, R.anim.slide_out_to_left, args);
				break;
		}

	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater){
		super.onCreateOptionsMenu(menu, menuInflater);
		MenuItem mniExport= menu.add("Export");
	}

	@Override
	public void onBackPressed() {
		((MainBaseSlidingActvity)getActivity()).postBackPressed(null);
	}

}
